package com.click.app.webapp.action;

import com.click.app.model.LabelValue;

import de.laures.cewolf.DatasetProduceException;
import java.text.SimpleDateFormat;
import com.click.app.model.Material;
import com.click.app.service.MaterialManager;
import com.click.app.service.PurchaseDetailManager;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.click.app.Constants;
import com.click.app.Functions;
import com.click.app.webapp.producers.ImageReportMaterial;
import com.click.app.webapp.reportObjects.ReportMaterialRow;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author click
 */
public class MaterialReportAction extends BaseAction {

    private static final long serialVersionUID = 295460450224891051L;
    private MaterialManager materialManager;
    private PurchaseDetailManager purchaseDetailManager;
    private ImageReportMaterial imageGenerate;
    private Long[] materials;
    private Date initDate;
    private Date endDate;
    private String dateStart;
    private String dateEnd;
    private String yearStart;
    private String yearEnd;
    private List<LabelValue> numYearList = new ArrayList<LabelValue>();
    private Map<String, String> materialListLeft;
    private Map<String, String> materialListRight;
    private Set<String> selectMaterials;
    private Map<String, String> typeReport;
    private String typeReportChoice;

    private List<ReportMaterialRow> listMaterialReport;
    private List listMaterialReportHeaders;

    private Number totalReport = 0;

    public String execute() {

        typeReport = new HashMap<String, String >();
        if (getRequest().getLocale().getLanguage().matches("en")){
        typeReport.put("1", "Per month");
        typeReport.put("2", "Per year");
        }else{
        typeReport.put("1", "Por mes");
        typeReport.put("2", "Por año");
        }
        SimpleDateFormat formatYear = new SimpleDateFormat("yyyy");
        Calendar currCalYearVar = Calendar.getInstance();
        Calendar currCalYear = Calendar.getInstance();
        currCalYear.add(Calendar.YEAR, -1);
        int j = 1;
        for (int i = currCalYear.get(Calendar.YEAR); i >= Constants.CONST_YEAR; i--, j++) {
            currCalYearVar.add(Calendar.YEAR, -1);
            String label = formatYear.format(currCalYearVar.getTime());
            numYearList.add(new LabelValue(label, j + ""));
        }

        materialListLeft = Functions.getHashMapFromLabelValue(materialManager.getMaterialLabelValue());

        materialListRight = new HashMap<String, String>();

        return SUCCESS;
    }

    public MaterialManager getMaterialManager() {
        return materialManager;
    }

    public void setMaterialManager(MaterialManager materialManager) {
        this.materialManager = materialManager;
    }

    public List<LabelValue> getNumYearList() {
        return numYearList;
    }

    public void setNumYearList(List<LabelValue> numYearList) {
        this.numYearList = numYearList;
    }

    public String getDateEnd() {
        return dateEnd;
    }

    public void setDateEnd(String dateEnd) {
        this.dateEnd = dateEnd;
    }

    public String getDateStart() {
        return dateStart;
    }

    public void setDateStart(String dateStart) {
        this.dateStart = dateStart;
    }

    public String getYearEnd() {
        return yearEnd;
    }

    public void setYearEnd(String yearEnd) {
        this.yearEnd = yearEnd;
    }

    public String getYearStart() {
        return yearStart;
    }

    public void setYearStart(String yearStart) {
        this.yearStart = yearStart;
    }

    public String reportMaterialPeriodSelect() throws DatasetProduceException {

        if (selectMaterials != null) {

            materials = Functions.getArrayLongFromArrayObjects(selectMaterials.toArray());

            boolean typePeriodo = false;
            if (typeReportChoice.equals("1")) {
                initDate = new Date(dateStart);
                endDate = new Date(dateEnd);

            }

            if (typeReportChoice.equals("2")) {
                initDate = new Date("01/01/" + yearStart);
                endDate = new Date("12/31/" + yearEnd);
                typePeriodo = true;
            }
            if(initDate.compareTo(endDate) <= 0){
                imageGenerate = new ImageReportMaterial(materialManager, purchaseDetailManager, materials, typePeriodo, initDate, endDate);
                imageGenerate.produceDataset(new HashMap());
                listMaterialReport = new ArrayList(imageGenerate.getReportMaterialRows().values());
                totalReport = 0;
                for(ReportMaterialRow rmr : listMaterialReport){
                    totalReport = totalReport.floatValue() + rmr.getTotalValue().floatValue();
                }
                if(listMaterialReport != null){
                    listMaterialReportHeaders = listMaterialReport.get(0).getCategories();
                }
            }else{
                 imageGenerate = null;
                 listMaterialReport = null;
                 listMaterialReportHeaders = null;
                 return ERROR;
            }
        }
        return SUCCESS;
    }

    public String typeReportChoice() {
        getSession().setAttribute("typeReportChoice", typeReportChoice);
        return SUCCESS;
    }

    public String refreshImageGenerate() {
        return SUCCESS;
    }

    public List<Material> getMaterialList() {
        return materialManager.getAll();
    }

    public ImageReportMaterial getImageGenerate() {
        if (imageGenerate == null)
            imageGenerate = (ImageReportMaterial) getSession().getAttribute("imageGenerate");
        
        return imageGenerate;
    }

    public void setImageGenerate(ImageReportMaterial imageGenerate) {
        this.imageGenerate = imageGenerate;
    }

    public void setPurchaseDetailManager(PurchaseDetailManager purchaseDetailManager) {
        this.purchaseDetailManager = purchaseDetailManager;
    }

    public Date getInitDate() {
        return initDate;
    }

    public void setInitDate(Date initDate) {
        this.initDate = initDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public Long[] getMaterials() {
        return materials;
    }

    public void setMaterials(Long[] materials) {
        this.materials = materials;
    }

    public Map<String, String> getTypeReport() {
        return typeReport;
    }

    public void setTypeReport(Map<String, String> typeReport) {
        this.typeReport = typeReport;
    }

    public String getTypeReportChoice() {
        if (typeReportChoice == null)
            typeReportChoice = getSession().getAttribute("typeReportChoice").toString();

        return typeReportChoice;
    }

    public void setTypeReportChoice(String typeReportChoice) {
        this.typeReportChoice = typeReportChoice;
    }

    public Map<String, String> getMaterialListLeft() {
        return materialListLeft;
    }

    public void setMaterialListLeft(Map<String, String> materialListLeft) {
        this.materialListLeft = materialListLeft;
    }

    public Map<String, String> getMaterialListRight() {
        return materialListRight;
    }

    public void setMaterialListRight(Map<String, String> materialListRight) {
        this.materialListRight = materialListRight;
    }

    public Set<String> getSelectMaterials() {
        return selectMaterials;
    }

    public void setSelectMaterials(Set<String> selectMaterials) {
        this.selectMaterials = selectMaterials;
    }

    public List<ReportMaterialRow> getListMaterialReport() {
        return listMaterialReport;
    }

    public void setListMaterialReport(List<ReportMaterialRow> listMaterialReport) {
        this.listMaterialReport = listMaterialReport;
    }

    public List getListMaterialReportHeaders() {
        return listMaterialReportHeaders;
    }

    public void setListMaterialReportHeaders(List listMaterialReportHeaders) {
        this.listMaterialReportHeaders = listMaterialReportHeaders;
    }

    public Number getTotalReport() {
        return totalReport;
    }

    public void setTotalReport(Number totalReport) {
        this.totalReport = totalReport;
    }

    public String help(){
        return SUCCESS;
    }
}
